package com.onpositive.image.tester;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;

import com.onpositive.image.fitting.tools.BasicImageFitter;

public class ResizeTester {
	
	public static void testBasic( String srcPath, String dstPath, int width, int height )
	{
		BufferedImage source ;
		try {
			source = ImageIO.read( new File(srcPath) ) ;
		} catch (IOException e) {
			e.printStackTrace();
			return ;
		} 
		BasicImageFitter bim = new BasicImageFitter() ;
		
		BufferedImage result0 = bim.fitImage( source, width, height ) ;
		BufferedImage result1 = bim.fitImage( source, width*2, height*2 ) ;
		
//		try {
//			ImageIO.write( result, "JPG", new File(dstPath)) ;
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg") ;
		ImageWriter defaultWriter = iter.next() ;
		
		ImageWriteParam iwp = defaultWriter.getDefaultWriteParam();
		iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		iwp.setCompressionQuality( 0.8f );
		
		//FileImageOutputStream output;
		
		
		FileOutputStream fs = null ;
		try {
			fs = new FileOutputStream( new File(dstPath+"_0.jpg") ) ;
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ByteArrayOutputStream byteArrOS = new ByteArrayOutputStream() ;
		byteArrOS.reset() ;
//		try {
//			fs = new FileOutputStream( new File(dstPath) ) ;
//		} catch (FileNotFoundException e2) {
//			e2.printStackTrace();			
//		}
		
		MemoryCacheImageOutputStream cacheImageStream = new MemoryCacheImageOutputStream( byteArrOS ) ;
//		try {
//			output = new FileImageOutputStream( new File(dstPath) );
//		} catch (FileNotFoundException e1) {
//			e1.printStackTrace();
//			return ;
//		} catch (IOException e1) {
//			e1.printStackTrace();
//			return ;
//		}
		//defaultWriter.setOutput(output);
		defaultWriter.setOutput( cacheImageStream );
		IIOImage image0 = new IIOImage( result0, null, null);
		IIOImage image1 = new IIOImage( result1, null, null);
		
		try {
			
			
			defaultWriter.write( null, image0, iwp ) ;
			long longOff = cacheImageStream.length() ;
			int offset0 = (int)longOff ;
			//cacheImageStream.flush() ;
			int length0 = byteArrOS.size() ;
			
			
			defaultWriter.write( null, image1, iwp ) ;
			longOff = cacheImageStream.length() ;
			int offset1 = (int)longOff ;

			//cacheImageStream.flush() ;
			int length1 = offset1 - offset0 ;
			
			byte []totalByteArr = byteArrOS.toByteArray() ;
			
			byte[] bArr0 = new byte[ length0 ] ;
			byte[] bArr1 = new byte[ length1 ] ;
			
			System.arraycopy( totalByteArr, 	0  , bArr0, 0, length0 ) ;
			System.arraycopy( totalByteArr, offset0, bArr1, 0, length1 ) ;
			
			fs.write(bArr0) ;
			fs.close() ;
			
			fs = new FileOutputStream( new File(dstPath+"_1.jpg") ) ;
			fs.write(bArr1) ;			
			fs.close() ;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
	}

}
